using UnityEngine;
using UnityEditor;

using Prota.Unity;
using System.Linq;
using System;

namespace Prota.Editor
{
    [CustomEditor(typeof(SpriteGroupTool), false)]
    [CanEditMultipleObjects]
    public class SpriteGroupToolInspector : UnityEditor.Editor
    {
        public override void OnInspectorGUI()
        {
            Undo.RecordObjects(targets, "SpriteGroupTool");
            
            ProtaEditorUtils.SeperateLine(2);
            
            var declineRecursive = serializedObject.FindProperty("declineRecursive");
            EditorGUILayout.PropertyField(declineRecursive);
            
            ProtaEditorUtils.SeperateLine(2);
            var setDepth = serializedObject.FindProperty("setDepth");
            EditorGUILayout.PropertyField(setDepth);
            if(setDepth.boolValue)
            {
                var applyDepthOnSelf = serializedObject.FindProperty("applyDepthOnSelf");
                EditorGUILayout.PropertyField(applyDepthOnSelf);
                
                var depthFrom = serializedObject.FindProperty("depthFrom");
                EditorGUILayout.PropertyField(depthFrom);
                
                var depthStep = serializedObject.FindProperty("depthStep");
                EditorGUILayout.PropertyField(depthStep);
                
                var depthForward = serializedObject.FindProperty("depthForward");
                EditorGUILayout.PropertyField(depthForward);
            }
            
            ProtaEditorUtils.SeperateLine(2);
            var setSortingLayer = serializedObject.FindProperty("setSortingLayer");
            EditorGUILayout.PropertyField(setSortingLayer);
            if(setSortingLayer.boolValue)
            {
                var layers = SortingLayer.layers.Select(x => x.name).ToArray();
                var layerIndex = serializedObject.FindProperty("sortingLayerIndex");
                layerIndex.intValue = EditorGUILayout.Popup("SortingLayer", layerIndex.intValue, layers);
            }
            
            ProtaEditorUtils.SeperateLine(2);
            var setLayer = serializedObject.FindProperty("setLayer");
            EditorGUILayout.PropertyField(setLayer);
            if(setLayer.boolValue)
            {
                var layerIndex = serializedObject.FindProperty("layerIndex");
            }
            
            ProtaEditorUtils.SeperateLine(2);
            var setParallax = serializedObject.FindProperty("setParallax");
            EditorGUILayout.PropertyField(setParallax);
            if(setParallax.boolValue)
            {
                var parallaxCameraAnchor = serializedObject.FindProperty("parallaxCameraAnchor");
                EditorGUILayout.PropertyField(parallaxCameraAnchor);
                
                var parallaxForward = serializedObject.FindProperty("parallaxForward");
                EditorGUILayout.PropertyField(parallaxForward);
                
                var parallaxStart = serializedObject.FindProperty("parallaxStart");
                EditorGUILayout.PropertyField(parallaxStart);
                
                var parallaxDistance = serializedObject.FindProperty("parallaxDistance");
                EditorGUILayout.PropertyField(parallaxDistance);
                
                var parallaxDistanceCurve = serializedObject.FindProperty("parallaxDistanceCurve");
                EditorGUILayout.PropertyField(parallaxDistanceCurve);
            }
            
            
            ProtaEditorUtils.SeperateLine(2);
            
            if(GUILayout.Button("Update All") || serializedObject.hasModifiedProperties)
            {
                serializedObject.ApplyModifiedProperties();
                
                foreach(var tgt in targets)
                {
                    (tgt as SpriteGroupTool).UpdateAll();
                }
                
            }
        }
        
        
        
    }
}
